// 这个文件会帮我们打包packages下的模块，最终打包出js文件
import minimist from "minimist"
import { resolve, dirname } from "path"
import { fileURLToPath } from "url"
import { createRequire } from "module"
import esbuild from "esbuild"
const require = createRequire(import.meta.url)
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const args = minimist(process.argv.slice(2))
// console.log(args)
const target = args._[0] || "reactivity" //打包哪个项目，默认为reactivity
const format = args.f || "iife" //打包后的模块化规范，默认为iife
// console.log(target, format)
const entry = resolve(__dirname, `../packages/${target}/src/index.ts`)
const pkg = require(`../packages/${target}/package.json`)
esbuild
  .context({
    entryPoints: [entry],
    bundle: true,
    outfile: resolve(__dirname, `../packages/${target}/dist/${target}.js`),
    format,
    platform: "browser",
    sourcemap: true,
    globalName: pkg.buildOptions?.name
  })
  .then(ctx => {
    console.log(`start dev ${target} successfully!`)
    return ctx.watch()
  })
